package com.seocoo.worker.order;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.seocoo.worker.R;
import com.seocoo.worker.base.http.BaseRsp;
import com.seocoo.worker.base.refresh.SwipeRefreshMoreLayout;
import com.seocoo.worker.base.widget.AppToolBer;
import com.seocoo.worker.friend.FriendSearchActivity;
import com.seocoo.worker.friend.entity.RxRefreshSuccess;
import com.seocoo.worker.order.adapter.OrderRelevanceWorkmateAdapter;
import com.seocoo.worker.order.entity.RspOrderIWorkmateInfo;
import com.seocoo.worker.order.model.OrderRelevanceWorkmateModel;
import com.seocoo.worker.order.req.ReqAddWorker;
import com.seocoo.worker.order.req.ReqAddWorkers;
import com.seocoo.worker.order.req.ReqRemoveWorker;
import com.sfh.lib.event.BusEventManager;
import com.sfh.lib.mvvm.LiveDataMatch;
import com.sfh.lib.ui.MVVMActivity;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;

/**
 * author : Ssp
 * date   : 2020/2/22 23:54
 * desc   : 关联工友
 */
public class OrderRelevanceWorkmateActivity extends MVVMActivity {

    private SwipeRefreshMoreLayout swipe_refresh_more_layout;
    private TextView tv_sure;
    private OrderRelevanceWorkmateAdapter adapter;
    private String installId;
    private AppToolBer appToolBer;

    public static void startContentUI(Context context, String installId) {
        Intent intent = new Intent(context, OrderRelevanceWorkmateActivity.class);
        intent.putExtra("installId", installId);
        context.startActivity(intent);
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.order_relevance_workmate_activity);
        installId = getIntent().getStringExtra("installId");
        initView();

        BusEventManager.register(String.class, s -> {
            if (TextUtils.isEmpty(s)) {
                return;
            }
            Gson gson = new Gson();
            Type type = new TypeToken<ArrayList<RspOrderIWorkmateInfo>>() {
            }.getType();
            ArrayList<RspOrderIWorkmateInfo> data = gson.fromJson(s, type);
            swipe_refresh_more_layout.onRefreshCompale(data, true);
            swipe_refresh_more_layout.onLoadAllCompale();
            tv_sure.setVisibility(View.VISIBLE);
        });
    }

    private void initView() {
        swipe_refresh_more_layout = findViewById(R.id.swipe_refresh_more_layout);
        appToolBer = findViewById(R.id.appToolBer);
        tv_sure = findViewById(R.id.tv_sure);
        adapter = new OrderRelevanceWorkmateAdapter();
        swipe_refresh_more_layout.setAdapter(adapter, false);
        swipe_refresh_more_layout.setRefreshing(false);
        swipe_refresh_more_layout.setOnLoadingListener(nowPage -> {
            swipe_refresh_more_layout.onLoadAllCompale();
        });
        swipe_refresh_more_layout.setOnItemChildClickListener((adapter, view, position) -> {
            RspOrderIWorkmateInfo item = (RspOrderIWorkmateInfo) adapter.getData().get(position);
            ReqRemoveWorker worker = new ReqRemoveWorker(item.getUserId(), installId);
            getViewModel(OrderRelevanceWorkmateModel.class).removeWorks(worker, position);
        });
        View inflate = getLayoutInflater().inflate(R.layout.add_works_empty_view, null);
        TextView tvAdd = inflate.findViewById(R.id.tv_add);
        tvAdd.setOnClickListener(v -> {
            List<RspOrderIWorkmateInfo> data = adapter.getData();
            FriendSearchActivity.start(OrderRelevanceWorkmateActivity.this, data);
        });
        swipe_refresh_more_layout.setEmptyView(inflate);
        getViewModel(OrderRelevanceWorkmateModel.class).queryWorker(installId);
        tv_sure.setOnClickListener(v -> checkData());
        appToolBer.getTvRight().setOnClickListener(v -> {
            List<RspOrderIWorkmateInfo> data = adapter.getData();
            FriendSearchActivity.start(OrderRelevanceWorkmateActivity.this, data);
        });
    }

    @LiveDataMatch
    public void onFriendList(BaseRsp<List<RspOrderIWorkmateInfo>> data) {
        if (data == null) {
            tv_sure.setVisibility(View.GONE);
            swipe_refresh_more_layout.onRefreshCompale(new ArrayList<RspOrderIWorkmateInfo>(), false);
            swipe_refresh_more_layout.onLoadAllCompale();
            return;
        }
        List<RspOrderIWorkmateInfo> infos = data.getData();
        if (infos == null || infos.size() == 0) {
            tv_sure.setVisibility(View.GONE);
            swipe_refresh_more_layout.onRefreshCompale(infos, false);
            swipe_refresh_more_layout.onLoadAllCompale();
            return;
        }

        tv_sure.setVisibility(View.VISIBLE);
        swipe_refresh_more_layout.onRefreshCompale(infos, false);
        swipe_refresh_more_layout.onLoadAllCompale();

    }

    private void checkData() {
        List<RspOrderIWorkmateInfo> data = adapter.getData();
        ReqAddWorkers reqAddWorkers = new ReqAddWorkers();
        ArrayList<ReqAddWorker> list = new ArrayList<>();
        for (int i = 0; i < data.size(); i++) {
            EditText et_money = (EditText) adapter.getViewByPosition(i, R.id.et_money);
            assert et_money != null;
            String money = et_money.getText().toString().trim();
            if (TextUtils.isEmpty(money)) {
                showToast("请设置分成金额");
                return;
            }
            RspOrderIWorkmateInfo info = data.get(i);
            ReqAddWorker worker = new ReqAddWorker();
            worker.setUserId(info.getUserId());
            worker.setAmount(money);
            worker.setInstallId(installId);
            list.add(worker);
        }
        reqAddWorkers.setWorkList(list);
        getViewModel(OrderRelevanceWorkmateModel.class).addWorks(reqAddWorkers);
    }

    @LiveDataMatch
    public void onAddWorksSuccess() {
        RxRefreshSuccess rxRefreshSuccess = new RxRefreshSuccess();
        rxRefreshSuccess.setSuccess(true);
        BusEventManager.postEvent(rxRefreshSuccess);
        finish();
    }

    @LiveDataMatch
    public void onRemoveWorksSuccess(int position) {
        //删除成功 刷新列表
        adapter.remove(position);
    }

}
